package com.trgzs.bate.common.utils;

import com.trgzs.bate.common.constant.Constants;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * @Author: dongl
 * @Email: dong980514280@gmail.com
 * @Create: 2025-01-07 22:21:32
 * @PS 全局链路id生成工具类
 **/
public class RequestIdUtil {

    public RequestIdUtil() {
    }

    public static void setRequestId() {
        //往MDC中存入UUID唯一标识
        MDC.put(Constants.TRACE_ID, UUID.randomUUID().toString());
    }

    public static void setRequestId(String requestId) {
        MDC.put(Constants.TRACE_ID, requestId);
    }

    public static String getRequestId() {
        return MDC.get(Constants.TRACE_ID);
    }

    public static void clear() {
        //需要释放，避免OOM
        MDC.clear();
    }
}